package com.fhc.view

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Check
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MaterialTheme.colorScheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.SheetState
import androidx.compose.material3.SheetValue
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.fhc.view.utils.rememberScreenHeight
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch


@Composable
fun rememberFhcBottomSheetVisibleState() = remember { mutableStateOf(false) }
// 定义行为枚举
enum class DismissType {
    DismissSheet,        // 关闭整个 bottom sheet
    DismissKeyboardOnly, // 只关闭键盘
    DoNothing            // 什么都不做
}
/**
 * Fhc bottom sheet
 *
 * @param title title of Sheet
 * @param sheetState Sheet stat
 * @param visibleState visible stat of Sheet
 * @param coroutineScope
 * @param onActionYes if true sheet will be hide
 * @param content  content of sheet
 * @receiver
 * @receiver
 */
@OptIn(ExperimentalMaterial3Api::class)
@Preview
@Composable
fun FhcBottomSheet( title: String = "",
                    showActionYes:Boolean = false,
                    sheetState:SheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true),
                    visibleState : MutableState<Boolean> = rememberFhcBottomSheetVisibleState(),
                    coroutineScope: CoroutineScope = rememberCoroutineScope(),
                    onActionYes:()->Boolean = {true},
                    onDismiss:(() -> Unit)? = null,
                    content: @Composable (ColumnScope.() -> Unit) = {}){
    val keyboardController = LocalSoftwareKeyboardController.current
    val focusManager = LocalFocusManager.current

    val screenHeight = rememberScreenHeight()
    if (visibleState.value) {
        ModalBottomSheet(
            onDismissRequest = {
                coroutineScope.launch {
                    sheetState.hide().run {
                        visibleState.value = false
                    }
                }
                onDismiss?.invoke()
            },
            sheetState = sheetState,
            scrimColor = Color.Transparent
        ) {
            // 在整个内容区域添加点击处理器来关闭键盘
            Box(
                modifier = Modifier.fillMaxWidth()
                    .pointerInput(Unit) {
                        detectTapGestures(
                            onTap = {
                                // 点击任意位置时关闭键盘,不工作？？？？
                                keyboardController?.hide()
                                focusManager.clearFocus()
                            }
                        )
                    }
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    if (!title.trim().isEmpty()) {
                        Box(modifier = Modifier.fillMaxWidth().padding(bottom = 8.dp)) {
                            Text(
                                modifier = Modifier.fillMaxWidth().wrapContentHeight(align = Alignment.CenterVertically),
                                textAlign = TextAlign.Center,
                                text = title,
                                style = MaterialTheme.typography.titleLarge,
                                fontWeight = FontWeight.Bold,
                                color = Color.Black
                            )
                        }
                    }
                    if (showActionYes) {
                        Row(modifier = Modifier.fillMaxWidth().padding(start = 8.dp, end = 8.dp, top = 8.dp)) {
                            Spacer(modifier = Modifier.weight(1f))
                            IconButton(
                                modifier = Modifier.wrapContentSize(),
                                onClick = {
                                    if (onActionYes()) {
                                        coroutineScope.launch {
                                            sheetState.hide().run {
                                                visibleState.value = false
                                            }
                                        }
                                    }
                                }) {
                                Box(modifier = Modifier.wrapContentSize(align = Alignment.Center)) {
                                    Icon(Icons.Outlined.Check, null, tint = colorScheme.primary, modifier = Modifier.size(32.dp))
                                }
                            }
                        }
                    }
                    HorizontalDivider(modifier = Modifier.fillMaxWidth().padding(start = 8.dp, end = 8.dp), thickness = 1.dp, color = colorScheme.primary)
                    Column(
                        modifier = Modifier.padding(8.dp).fillMaxWidth().heightIn(max = screenHeight * 0.70f)
                            .verticalScroll(rememberScrollState()),
                        verticalArrangement = Arrangement.spacedBy(18.dp)
                    ) {
                        content()
                    }
                }
            }
        }
    }

}


